from ..common import *

class CollapsibleAliasManage(wx.Panel):

    btn_info = '文件别名设置（回车保存，中间用逗号隔开）'

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)

        self._init__gui()

        '''
            重要！！！必须先展开，以便让父容器感知到存在（不知道是不是版本BUG）
        '''
        self.collapsPanel.Collapse(self.collapsPanel.IsExpanded()) # 展开操作
        self.Layout()

    def _init__gui(self):
        
        self.collapsPanel = PCP.PyCollapsiblePane(self, label=f"{self.btn_info}【点击显示】", agwStyle=wx.CP_NO_TLW_RESIZE)
        self.sizer.Add(self.collapsPanel, 1, wx.EXPAND | wx.ALL, 0)
        collapsiblePanel = self.collapsPanel.GetPane()
        
        '''
            网格布局
        '''
        flexGridSizer = wx.FlexGridSizer(cols=3, hgap=1, vgap=5)
        flexGridSizer.AddGrowableCol(0)
        flexGridSizer.AddGrowableCol(1)
        flexGridSizer.AddGrowableCol(2) # 三列均分

        '''
            视图别名
        '''
        sizerView = wx.BoxSizer(wx.VERTICAL)
        sizerView.Add(wx.StaticText(collapsiblePanel, -1, '视图文件默认名称：views.py'), 0, wx.EXPAND | wx.ALL, 2)
        self.inputAliasView = wx.TextCtrl(collapsiblePanel, -1, value=','.join(env.get_file_alias('views')), style=wx.TE_PROCESS_ENTER)
        sizerView.Add(self.inputAliasView, 0, wx.EXPAND | wx.ALL, 2)
        
        '''
            模型别名
        '''
        sizerModel = wx.BoxSizer(wx.VERTICAL)
        sizerModel.Add(wx.StaticText(collapsiblePanel, -1, '模型文件默认名称：models.py'), 0, wx.EXPAND | wx.ALL, 2)
        self.inputAliasModel = wx.TextCtrl(collapsiblePanel, -1, value=','.join(env.get_file_alias('models')), style=wx.TE_PROCESS_ENTER)
        sizerModel.Add(self.inputAliasModel, 1, wx.EXPAND | wx.ALL, 2)
        
        '''
            路由别名
        '''
        sizerUrl = wx.BoxSizer(wx.VERTICAL)
        sizerUrl.Add(wx.StaticText(collapsiblePanel, -1, '路由文件默认名称：urls.py'), 0, wx.EXPAND | wx.ALL, 2)
        self.inputAliasUrl = wx.TextCtrl(collapsiblePanel, -1, value=','.join(env.get_file_alias('urls')), style=wx.TE_PROCESS_ENTER)
        sizerUrl.Add(self.inputAliasUrl, 1, wx.EXPAND | wx.ALL, 2)

        flexGridSizer.Add(sizerView, 0, wx.EXPAND)
        flexGridSizer.Add(sizerModel, 0, wx.EXPAND)
        flexGridSizer.Add(sizerUrl, 0, wx.EXPAND)

        '''
            包装网格布局，添加进折叠布局中（重要！！！否则无法正常显示控件！！！）
        '''
        border = wx.BoxSizer()
        border.Add(flexGridSizer, 1, wx.EXPAND|wx.ALL, 5)
        collapsiblePanel.SetSizer(border)

        # 注册监听事件
        self.Bind(wx.EVT_TEXT_ENTER, self.onInputEnter, self.inputAliasView)
        self.Bind(wx.EVT_TEXT_ENTER, self.onInputEnter, self.inputAliasModel)
        self.Bind(wx.EVT_TEXT_ENTER, self.onInputEnter, self.inputAliasUrl)
        
        # 折叠按钮监听
        self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.onDirsPaneChanged, self.collapsPanel)

    def onInputEnter(self, evt: wx.CommandEvent):
        '''监听回车键'''
        s: str = evt.GetString()
        # 数据校验
        if '，' in s:
            RichMsgDialog.showOkMsgDialog(self, '请使用英文格式下的逗号', '警告')
            return
        sp_s: List[str] = [_ for _ in s.split(',') if _.strip()]
        if evt.GetId() == self.inputAliasView.GetId():
            if 'views.py' not in sp_s:
                self.inputAliasView.SetValue(','.join(env.get_file_alias('views')))
                RichMsgDialog.showOkMsgDialog(self, 'views.py系统名不允许删除', '错误')
                return
            env.add_alias_for_files(sp_s, file_type='views')
        elif evt.GetId() == self.inputAliasModel.GetId():
            if 'models.py' not in sp_s:
                self.inputAliasModel.SetValue(','.join(env.get_file_alias('models')))
                RichMsgDialog.showOkMsgDialog(self, 'models.py系统名不允许删除', '错误')
                return
            env.add_alias_for_files(sp_s, file_type='models')
        elif evt.GetId() == self.inputAliasUrl.GetId():
            if 'urls.py' not in sp_s:
                self.inputAliasUrl.SetValue(','.join(env.get_file_alias('urls')))
                RichMsgDialog.showOkMsgDialog(self, 'urls.py系统名不允许删除', '错误')
                return
            env.add_alias_for_files(sp_s, file_type='urls')
        RichMsgDialog.showOkMsgDialog(self, '更新成功', '成功')

    def onDirsPaneChanged(self, evt):
        '''根路径 折叠按钮监听'''
        self.Layout()
        if self.collapsPanel.IsExpanded():
            self.collapsPanel.SetLabel(f"{self.btn_info}【点击隐藏】")
        else:
            self.collapsPanel.SetLabel(f"{self.btn_info}【点击显示】")
